package com.hw.ui.code_replace;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.hw.HWApp;
import com.hw.base.model.BaseModel;
import com.hw.base.util.Util;
import com.hw.bean.AbBean;
import com.hw.bean.AbQueryBean;
import com.hw.daobean.DataBaseControl;
import com.hw.daobean.RelationInfo;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

public class CodeReplaceModelImpl extends BaseModel implements CodeReplaceModel {
    private String tag = "CodeReplaceModelImpl";
    private String msg = "";
    private String casecode = "";
    private AbBean abBean;
    private AbQueryBean abQueryBean;

    public void postHandle(final CallBack listener, final int n) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                switch (n) {
                    case 0:
                        listener.oncodeQuery(msg);
                        break;
                    case 1:
                        listener.oncodeQueryFail(msg);
                        break;
                    case 2:
                        listener.oncodeReplace(msg);
                        break;
                    case 3:
                        listener.oncodeReplaceFail(msg);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    public void codeQuery(String code, final CallBack listener) {
        Request request = new Request.Builder().url(
                HWApp.sSharedPreferences.getString(Util.addk, "") + Util.urlsuffix +
                        "?action=Query&cid=" +
                        HWApp.sSharedPreferences.getString(Util.cidk, "") +
                        "&userID=" + HWApp.sSharedPreferences.getInt(Util.userID, 0) +
                        "&code=" + code).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                msg = Util.netConnectionError;
                postHandle(listener, 1);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {//回调的方法执行在子线程。
                    String result = response.body().string();
                    Log.e(tag, result);
                    try {
                        abQueryBean = gson.fromJson(result, AbQueryBean.class);
                        if (null != abQueryBean && abQueryBean.getErrcode() == 0) {
                            msg = abQueryBean.getData().getCaseCode();
                            casecode = abQueryBean.getData().getCaseCode();
                            postHandle(listener, 0);
                        } else {
                            msg = abQueryBean.getErrmsg();
                            postHandle(listener, 1);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        msg = Util.exceptionError;
                        postHandle(listener, 1);
                    }
                } else {
                    msg = Util.netServerError;
                    postHandle(listener, 1);
                }
            }
        });
    }

    @Override
    public void codeReplace(String oldcode, final String newcode, final CallBack listener) {
        Request request = new Request.Builder().url(
                HWApp.sSharedPreferences.getString(Util.addk, "") + Util.urlsuffix +
                        "?action=Replace&cid=" +
                        HWApp.sSharedPreferences.getString(Util.cidk, "") +
                        "&userID=" + HWApp.sSharedPreferences.getInt(Util.userID, 0) +
                        "&codeOld=" + oldcode +
                        "&codeNew=" + newcode).build();
        Log.e(tag,HWApp.sSharedPreferences.getString(Util.addk, "") + Util.urlsuffix +
                "?action=Replace&cid=" +
                HWApp.sSharedPreferences.getString(Util.cidk, "") +
                "&userID=" + HWApp.sSharedPreferences.getInt(Util.userID, 0) +
                "&codeOld=" + oldcode +
                "&codeNew=" + newcode);
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                msg = Util.netConnectionError;
                postHandle(listener, 3);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {//回调的方法执行在子线程。
                    String result = response.body().string();
                    Log.e(tag, result);
                    try {
                        abBean = gson.fromJson(result, AbBean.class);
                        if (null != abBean && abBean.getErrcode() == 0) {
                            msg = "盒码替换成功";
                            RelationInfo relationInfo = new RelationInfo(null,
                                    "-1",
                                    "-1",
                                    "-1",
                                    casecode,
                                    newcode
                            );
                            DataBaseControl.addRelation(relationInfo);
                            postHandle(listener, 2);
                        } else {
                            msg = abBean.getErrmsg();
                            postHandle(listener, 3);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        msg = Util.exceptionError;
                        postHandle(listener, 3);
                    }
                } else {
                    msg = Util.netServerError;
                    postHandle(listener, 3);
                }
            }
        });
    }
}

